﻿
namespace ASPItalia.ModelVirtualCasting.Web.RiaServices
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Data;
  using System.Linq;
  using System.ServiceModel.DomainServices.EntityFramework;
  using System.ServiceModel.DomainServices.Hosting;
  using System.ServiceModel.DomainServices.Server;
  using System.Threading;


  // Implements application logic using the ModelVirtualCastingEntities context.
  // TODO: Add your application logic to these methods or in additional methods.
  // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
  // Also consider adding roles to restrict access as appropriate.
  // [RequiresAuthentication]
  [EnableClientAccess()]
  public class ModelVirtualCastingDomainService : LinqToEntitiesDomainService<ModelVirtualCastingEntities>
  {

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Countries' query.
    public IQueryable<Countries> GetCountries()
    {
      return this.ObjectContext.Countries;
    }

    public void InsertCountries(Countries countries)
    {
      if ((countries.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(countries, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Countries.AddObject(countries);
      }
    }

    public void UpdateCountries(Countries currentCountries)
    {
      this.ObjectContext.Countries.AttachAsModified(currentCountries, this.ChangeSet.GetOriginal(currentCountries));
    }

    public void DeleteCountries(Countries countries)
    {
      if ((countries.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Countries.Attach(countries);
      }
      this.ObjectContext.Countries.DeleteObject(countries);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'JobLevels' query.
    public IQueryable<JobLevels> GetJobLevels()
    {
      return this.ObjectContext.JobLevels;
    }

    public void InsertJobLevels(JobLevels jobLevels)
    {
      if ((jobLevels.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(jobLevels, EntityState.Added);
      }
      else
      {
        this.ObjectContext.JobLevels.AddObject(jobLevels);
      }
    }

    public void UpdateJobLevels(JobLevels currentJobLevels)
    {
      this.ObjectContext.JobLevels.AttachAsModified(currentJobLevels, this.ChangeSet.GetOriginal(currentJobLevels));
    }

    public void DeleteJobLevels(JobLevels jobLevels)
    {
      if ((jobLevels.EntityState == EntityState.Detached))
      {
        this.ObjectContext.JobLevels.Attach(jobLevels);
      }
      this.ObjectContext.JobLevels.DeleteObject(jobLevels);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'JobTypes' query.
    public IQueryable<JobTypes> GetJobTypes()
    {
      return this.ObjectContext.JobTypes;
    }

    public void InsertJobTypes(JobTypes jobTypes)
    {
      if ((jobTypes.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(jobTypes, EntityState.Added);
      }
      else
      {
        this.ObjectContext.JobTypes.AddObject(jobTypes);
      }
    }

    public void UpdateJobTypes(JobTypes currentJobTypes)
    {
      this.ObjectContext.JobTypes.AttachAsModified(currentJobTypes, this.ChangeSet.GetOriginal(currentJobTypes));
    }

    public void DeleteJobTypes(JobTypes jobTypes)
    {
      if ((jobTypes.EntityState == EntityState.Detached))
      {
        this.ObjectContext.JobTypes.Attach(jobTypes);
      }
      this.ObjectContext.JobTypes.DeleteObject(jobTypes);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Languages' query.
    public IQueryable<Languages> GetLanguages()
    {
      return this.ObjectContext.Languages;
    }

    public void InsertLanguages(Languages languages)
    {
      if ((languages.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(languages, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Languages.AddObject(languages);
      }
    }

    public void UpdateLanguages(Languages currentLanguages)
    {
      this.ObjectContext.Languages.AttachAsModified(currentLanguages, this.ChangeSet.GetOriginal(currentLanguages));
    }

    public void DeleteLanguages(Languages languages)
    {
      if ((languages.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Languages.Attach(languages);
      }
      this.ObjectContext.Languages.DeleteObject(languages);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Multimedia' query.
    public IQueryable<Multimedia> GetMultimedia()
    {
      return this.ObjectContext.Multimedia;
    }

    public void InsertMultimedia(Multimedia multimedia)
    {
      if ((multimedia.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(multimedia, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Multimedia.AddObject(multimedia);
      }
    }

    public void UpdateMultimedia(Multimedia currentMultimedia)
    {
      this.ObjectContext.Multimedia.AttachAsModified(currentMultimedia, this.ChangeSet.GetOriginal(currentMultimedia));
    }

    public void DeleteMultimedia(Multimedia multimedia)
    {
      if ((multimedia.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Multimedia.Attach(multimedia);
      }
      this.ObjectContext.Multimedia.DeleteObject(multimedia);
    }
    
    [Query(IsComposable = false)]
    public IQueryable<Multimedia> GetMultimediaOfPeople(int peopleId)
    {
      return this.ObjectContext.Multimedia.Include("People").Where(m => m.People.Contains(m.People.FirstOrDefault(p => p.ID == peopleId)));
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'News' query.
    public IQueryable<News> GetNews()
    {
      return this.ObjectContext.News;
    }

    public void InsertNews(News news)
    {
      if ((news.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(news, EntityState.Added);
      }
      else
      {
        this.ObjectContext.News.AddObject(news);
      }
    }

    public void UpdateNews(News currentNews)
    {
      this.ObjectContext.News.AttachAsModified(currentNews, this.ChangeSet.GetOriginal(currentNews));
    }

    public void DeleteNews(News news)
    {
      if ((news.EntityState == EntityState.Detached))
      {
        this.ObjectContext.News.Attach(news);
      }
      this.ObjectContext.News.DeleteObject(news);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'People' query.
    public IQueryable<People> GetPeople()
    {
      Thread.Sleep(1000);
      return this.ObjectContext.People.Include("Multimedia");
    }

    public void InsertPeople(People people)
    {
      if ((people.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(people, EntityState.Added);
      }
      else
      {
        this.ObjectContext.People.AddObject(people);
      }
    }

    public void UpdatePeople(People currentPeople)
    {
      this.ObjectContext.People.AttachAsModified(currentPeople, this.ChangeSet.GetOriginal(currentPeople));
    }

    public void DeletePeople(People people)
    {
      if ((people.EntityState == EntityState.Detached))
      {
        this.ObjectContext.People.Attach(people);
      }
      this.ObjectContext.People.DeleteObject(people);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Requests' query.
    public IQueryable<Requests> GetRequests()
    {
      return this.ObjectContext.Requests;
    }

    public void InsertRequests(Requests requests)
    {
      if ((requests.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(requests, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Requests.AddObject(requests);
      }
    }

    public void UpdateRequests(Requests currentRequests)
    {
      this.ObjectContext.Requests.AttachAsModified(currentRequests, this.ChangeSet.GetOriginal(currentRequests));
    }

    public void DeleteRequests(Requests requests)
    {
      if ((requests.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Requests.Attach(requests);
      }
      this.ObjectContext.Requests.DeleteObject(requests);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Roles' query.
    public IQueryable<Roles> GetRoles()
    {
      return this.ObjectContext.Roles;
    }

    public void InsertRoles(Roles roles)
    {
      if ((roles.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(roles, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Roles.AddObject(roles);
      }
    }

    public void UpdateRoles(Roles currentRoles)
    {
      this.ObjectContext.Roles.AttachAsModified(currentRoles, this.ChangeSet.GetOriginal(currentRoles));
    }

    public void DeleteRoles(Roles roles)
    {
      if ((roles.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Roles.Attach(roles);
      }
      this.ObjectContext.Roles.DeleteObject(roles);
    }

    // TODO:
    // Consider constraining the results of your query method.  If you need additional input you can
    // add parameters to this method or create additional query methods with different names.
    // To support paging you will need to add ordering to the 'Users' query.
    public IQueryable<Users> GetUsers()
    {
      return this.ObjectContext.Users;
    }

    public void InsertUsers(Users users)
    {
      if ((users.EntityState != EntityState.Detached))
      {
        this.ObjectContext.ObjectStateManager.ChangeObjectState(users, EntityState.Added);
      }
      else
      {
        this.ObjectContext.Users.AddObject(users);
      }
    }

    public void UpdateUsers(Users currentUsers)
    {
      this.ObjectContext.Users.AttachAsModified(currentUsers, this.ChangeSet.GetOriginal(currentUsers));
    }

    public void DeleteUsers(Users users)
    {
      if ((users.EntityState == EntityState.Detached))
      {
        this.ObjectContext.Users.Attach(users);
      }
      this.ObjectContext.Users.DeleteObject(users);
    }
  }
}


